package com.bed.lovewithpatientbed.controller;

import com.bed.lovewithpatientbed.common.bean.DeptDetailBean;
import com.bed.lovewithpatientbed.common.bean.LayUiPageBean;
import com.bed.lovewithpatientbed.core.annocation.LogAnno;
import com.bed.lovewithpatientbed.pojo.BedPut;
import com.bed.lovewithpatientbed.pojo.Dept;
import com.bed.lovewithpatientbed.pojo.DeptManager;
import com.bed.lovewithpatientbed.pojo.Hospital;
import com.bed.lovewithpatientbed.service.BedService;
import com.bed.lovewithpatientbed.service.DeptService;
import com.bed.lovewithpatientbed.service.HospitalService;
import com.bed.lovewithpatientbed.util.LayUiUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yg
 * @version 1.0.0
 * @ClassName HospitalService.java
 * @Description TODO
 */
@Controller
@RequestMapping("dept")
public class DeptManagerController {

    @Resource
    private DeptService deptService;

    @Resource
    private BedService bedService;

    @LogAnno(operateType = "执行")
    @RequestMapping("list")
    public String list(){
        return "dept/list";
    }

    @LogAnno(operateType = "查询展示")
    @RequestMapping("listDo")
    @ResponseBody
    public Map<String,Object> deptListDo( Integer hospitalId){
        Map<String,Object> resultMap=new HashMap<>();

        List<DeptDetailBean> deptDetailBeanList=new ArrayList<>();
        List<Dept> deptList=deptService.getAll();

        for (Dept dept:deptList
             ) {
            DeptDetailBean deptDetailBean=new DeptDetailBean();
            //封装科室展示信息
            deptDetailBean.setDeptId(dept.getDeptId());
            deptDetailBean.setDeptName(dept.getDeptName());

            DeptManager deptManager = deptService.selectByDeptIdHospitalId(dept.getDeptId(), hospitalId);
            deptDetailBean.setDeptManager(deptManager.getDeptManagerName());

            List<BedPut> bedPutList=bedService.selectByHospitalIdAndDeptId(dept.getDeptId(),hospitalId);
            int bedputcount=bedPutList.size();
            deptDetailBean.setBedcount(bedputcount);

            deptDetailBeanList.add(deptDetailBean);
        }
        long countRow=deptList.size();

        System.out.println("------------------------------------------");
        System.out.println(deptDetailBeanList);
        System.out.println("------------------------------------------");
        return LayUiUtil.getLayTableResponseMap("success",0,countRow,deptDetailBeanList);
    }

    @RequestMapping("add")
    public String add(){
        return "adddept";
    }
    //新增科室信息
    @RequestMapping("addDo")
    public Map<String,Object> addDept(Dept dept){
        Map<String,Object> resultMap=new HashMap<>();

        try{
            int i = deptService.addDept(dept);
            if(i>0){
                resultMap.put("result",true);
            }else {
                resultMap.put("result",false);
            }
            return resultMap;
        }catch (Exception e){
            e.printStackTrace();
            resultMap.put("result",false);
        }
        return resultMap;
    }

    //删除科室信息
    @RequestMapping("delDo")
    public Map<String,Object> delDept(Dept dept){
        Map<String,Object> resultMap=new HashMap<>();

        try{
            int i = deptService.delDept(dept);
            if(i>0){
                resultMap.put("result",true);
            }else {
                resultMap.put("result",false);
            }
            return resultMap;
        }catch (Exception e){
            e.printStackTrace();
            resultMap.put("result",false);
        }
        return resultMap;
    }


    @RequestMapping("update")
    public String update(){
        return "updatedept";
    }

    //修改科室信息
    @RequestMapping("updateDo")
    public Map<String,Object> updateDept(Dept dept){
        Map<String,Object> resultMap=new HashMap<>();

        try{
            int i = deptService.updateDept(dept);
            if(i>0){
                resultMap.put("result",true);
            }else {
                resultMap.put("result",false);
            }
            return resultMap;
        }catch (Exception e){
            e.printStackTrace();
            resultMap.put("result",false);
        }
        return resultMap;
    }

    /**
         * @title 查询科室具体详情
         * @description 根据医院id和科室id查询科室的信息，为了显示科室的床位数量
         * @author yg
         * @updateTime
         * @throws
         */

    @LogAnno(operateType = "查询科室具体信息")
    @RequestMapping("DeptInfo")
    public Map<String,Object> DeptInfo(Integer did){
        Map<String,Object> resultMap=new HashMap<>();
        try{
            Dept dept = deptService.selectById(did);
            resultMap.put("dept",dept);
            resultMap.put("result",true);
        }catch (Exception e){
            e.printStackTrace();
            resultMap.put("result",false);
        }
        return resultMap;
    }

}
